﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace LevenBlog.Core.Entities
{
    [Serializable]
    [DataContract]
    // Generic Pair class.  Overrides Equals() and GetHashCode(), so it can be used as a dictionary key.
    public sealed class Pair<TFirst, TSecond> {
        private TFirst _first;
        private TSecond _second;

        public Pair()
        {
            _first = default(TFirst);
            _second = default(TSecond);
        }

        public Pair(TFirst first, TSecond second) {
            _first = first;
            _second = second;
        }

        [DataMember]
        public TFirst First {
            get {
                return _first;
            }
            set
            {
                _first = value;
            }
        }

        [DataMember]
        public TSecond Second {
            get {
                return _second;
            }
            set { _second = value; }
        }

        public override bool Equals(object obj) {
            if (obj == this) {
                return true;
            }

            Pair<TFirst, TSecond> other = obj as Pair<TFirst, TSecond>;
            return (other != null) &&
                (((other._first == null) && (_first == null)) ||
                    ((other._first != null) && other._first.Equals(_first))) &&
                (((other._second == null) && (_second == null)) ||
                    ((other._second != null) && other._second.Equals(_second)));
        }

        public override int GetHashCode() {
            int a = (_first == null) ? 0 : _first.GetHashCode();
            int b = (_second == null) ? 0 : _second.GetHashCode();
            return CombineHashCodes(a, b);
        }

        // Copied from ndp\fx\src\xsp\System\Web\Util\HashCodeCombiner.cs
        private static int CombineHashCodes(int h1, int h2) {
            return ((h1 << 5) + h1) ^ h2;
        }
    }

}
